﻿
#ifndef __tuya_conn_mgr_h__
#define __tuya_conn_mgr_h__

#include "packet_buffer.h"
#include "uart_client.h"
#include "cmd_handler.h"
#include "project.h"

typedef std::function<void()> pop_callback;

typedef struct weatherInfo
{
    std::string weatherImg;
    std::string weatherName;
} WeatherInfo;

static const std::map<int, WeatherInfo> WeatherImgMap = {
    {101, {"@mipmap/tuyaWeather/dayu", "大雨"}},
    {102, {"@mipmap/tuyaWeather/lei", "雷暴"}},
    {103, {"@mipmap/tuyaWeather/yangsha", "沙尘暴"}},
    {104, {"@mipmap/tuyaWeather/xiaoxue", "小雪"}},
    {105, {"@mipmap/tuyaWeather/zhongxue", "雪"}},
    {106, {"@mipmap/tuyaWeather/wu", "冻雾"}},
    {107, {"@mipmap/tuyaWeather/dayu", "暴雨"}},
    {108, {"@mipmap/tuyaWeather/zhongyu", "局部阵雨"}},
    {109, {"@mipmap/tuyaWeather/yangsha", "浮尘"}},
    {110, {"@mipmap/tuyaWeather/lei", "雷电"}},
    {111, {"@mipmap/tuyaWeather/xiaoyu", "小阵雨"}},
    {112, {"@mipmap/tuyaWeather/zhongyu", "雨"}},
    {113, {"@mipmap/tuyaWeather/xuejiayu", "雨夹雪"}},
    {114, {"@mipmap/tuyaWeather/fengbao", "尘卷风"}},
    {115, {"@mipmap/tuyaWeather/xiaoxue", "冰粒"}},
    {116, {"@mipmap/tuyaWeather/yangsha", "强沙尘暴"}},
    {117, {"@mipmap/tuyaWeather/yangsha", "扬沙"}},
    {118, {"@mipmap/tuyaWeather/zhongyu", "小到中雨"}},
    {119, {"@mipmap/tuyaWeather/qingtian", "大部晴朗"}},
    {120, {"@mipmap/tuyaWeather/qingtian", "晴"}},
    {121, {"@mipmap/tuyaWeather/wu", "雾"}},
    {122, {"@mipmap/tuyaWeather/zhenyu", "阵雨"}},
    {123, {"@mipmap/tuyaWeather/zhenyu", "强阵雨"}},
    {124, {"@mipmap/tuyaWeather/daxue", "大雪"}},
    {125, {"@mipmap/tuyaWeather/dayu", "特大暴雨"}},
    {126, {"@mipmap/tuyaWeather/baoxue", "暴雪"}},
    {127, {"@mipmap/tuyaWeather/daxue", "冰雹"}},
    {128, {"@mipmap/tuyaWeather/zhongxue", "小到中雪"}},
    {129, {"@mipmap/tuyaWeather/shaoyun", "少云"}},
    {130, {"@mipmap/tuyaWeather/xiaoxue", "小阵雪"}},
    {131, {"@mipmap/tuyaWeather/zhongxue", "中雪"}},
    {132, {"@mipmap/tuyaWeather/yin", "阴"}},
    {133, {"@mipmap/tuyaWeather/xiaoxue", "冰针"}},
    {134, {"@mipmap/tuyaWeather/dayu", "大暴雨"}},
    {135, {"@mipmap/tuyaWeather/wind", ""}},
    {136, {"@mipmap/tuyaWeather/zhenyu", "雷阵雨伴有冰雹"}},
    {137, {"@mipmap/tuyaWeather/xuejiayu", "冻雨"}},
    {138, {"@mipmap/tuyaWeather/zhongxue", "阵雪"}},
    {139, {"@mipmap/tuyaWeather/xiaoyu", "小雨"}},
    {140, {"@mipmap/tuyaWeather/mai", "霾"}},
    {141, {"@mipmap/tuyaWeather/zhongyu", "中雨"}},
    {142, {"@mipmap/tuyaWeather/duoyun", "多云"}},
    {143, {"@mipmap/tuyaWeather/leizhenyu", "雷阵雨"}},
    {144, {"@mipmap/tuyaWeather/dayu", "中到大雨"}},
    {145, {"@mipmap/tuyaWeather/dayu", "大到暴雨"}},
    {146, {"@mipmap/tuyaWeather/qingtian", "晴朗"}},
};

static unsigned char *_cmd_temp = (unsigned char *)"w.temp";
static unsigned char *_cmd_weather = (unsigned char *)"w.conditionNum";

class CTuYaConnMgr : public EventHandler, public IHandler
{
protected:
    CTuYaConnMgr();
    ~CTuYaConnMgr();

public:
    static CTuYaConnMgr *ins()
    {
        static CTuYaConnMgr stIns;
        return &stIns;
    }

    std::string searchProductInfo();
    void send2TuYa();

    int init();
    // 发送给按键板
    void send2MCU(TUYA_WIFI_CMD_e cmd);
    // 声音关闭
    void closeVolumn(bool flag);
    // 炉灯开关
    void setStoveLight(bool flag);
    // 待机状态[时钟]
    void setSleep(bool flag);
    // 蜂鸣器声音
    void setBeep(bool flag = true, const char *from = 0);

    // 版本号
    bool getVersion(int &a, int &b, int &c);

protected:
    virtual int checkEvents();
    virtual int handleEvents();
    virtual void onCommDeal(IAck *ack);
    void onDpCommDeal(IAck *ack);
    void getLocalTime(IAck *ack);
    void getWeatherData(IAck *ack);
    void parseWeatherData(IAck *ack, std::map<std::string, std::string> *weatherMap);

private:
    // std::string pID = "qubke1nih6b87fi1";
    std::string pID = "3rnmb7mduigzd8nn";
    bool mIsFactory;
    int64_t mLastStatusTime;
    IPacketBuffer *mPacket;
    int64_t mNextEventTime;
    int64_t mLastSendTime;
    int64_t mLastWarnTime;
    int64_t mLastBeepTime;
    UartClient *mUartMCU; // mcu
    int mSetData;         // 设置数据次数
    int commErrorCount;
    uchar mSndData[UI2MCU::BUF_LEN]; // 发送缓冲区

    uchar mMcuType; // 机型

    bool isFirst = true;
    uchar mFunction[TUYA2UI::BUF_LEN];
    bool reqOpenWeatherFlag;
    int64_t mLastConnectTime;
};

extern CTuYaConnMgr *g_tuYaObjConnMgr;

#endif
